package com.example.controller.community;

import com.example.dto.request.group.InsertGroup;
import com.example.dto.business.DynamicCommentBusinessDTO;
import com.example.dto.request.community.DynamicCommentRequestDTO;
import com.example.dto.request.PageRequestDTO;
import com.example.pojo.User;
import com.example.response.ApiResult;
import com.example.service.user.UserAuthenticationService;
import com.example.vo.community.UserCommentsVO;
import com.example.service.user.CommunityDynamicCommentService;
import com.example.util.RequestParamUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author LeXin Huang
 * @date 2021年03月23日 14:15
 */
@Api(tags = "社区评论模块")
@Slf4j
@Validated
@RestController
public class DynamicCommentController {

    private final UserAuthenticationService userAuthenticationService;

    private final CommunityDynamicCommentService commentService;

    public DynamicCommentController(UserAuthenticationService userAuthenticationService,
                                    CommunityDynamicCommentService commentService) {
        this.userAuthenticationService = userAuthenticationService;
        this.commentService = commentService;
    }

    @ApiOperation(value = "发表评论")
    @PostMapping("/dynamics/{dynamicId}/comments")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户令牌", required = true,
                    dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(value = "动态id", required = true,
                    dataTypeClass = String.class, paramType = "path")
    })
    public ApiResult<ApiResult.ApiData> publishComment(@RequestHeader(value = "token") String token,
                                                       @Validated(value = InsertGroup.class)
                                                       @RequestBody DynamicCommentRequestDTO requestDTO,
                                                       @PathVariable("dynamicId") String dynamicIdParam) {
        User user = userAuthenticationService.login(token);
        Integer dynamicId = RequestParamUtil.parseParamAsInteger(dynamicIdParam);
        requestDTO.setUserId(user.getId());
        requestDTO.setDynamicId(dynamicId);
        commentService.addComment(requestDTO);
        return ApiResult.buildNonDataSuccessResponse("评论成功");
    }


    @GetMapping("/dynamics/{dynamicId}/comments/history")
    @ApiOperation(value = "下拉查看评论")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户令牌",
                    dataTypeClass = String.class, paramType = "header"),
            @ApiImplicitParam(value = "动态的id", required = true,
                    dataTypeClass = Integer.class, paramType = "path"),
            @ApiImplicitParam(value = "评论偏移id", required = true,
                    dataTypeClass = Integer.class, paramType = "path", defaultValue = "1")
    })
    public ApiResult<UserCommentsVO> pullDownDynamicComments(@RequestHeader(value = "token", required = false) String token,
                                                             @PathVariable("dynamicId") String dynamicIdPathParam,
                                                             @RequestParam("comment_offset_id") String commentOffsetIdParam) {
        userAuthenticationService.login(token);
        int dynamicId = RequestParamUtil.parseParamAsInteger(dynamicIdPathParam);
        int commentOffset = RequestParamUtil.parseParamAsInteger(commentOffsetIdParam);

        ApiResult<UserCommentsVO> response = new ApiResult<>();
        PageRequestDTO pageDTO = new PageRequestDTO();
        pageDTO.setOffset(commentOffset);
        pageDTO.setSize(8);
        List<DynamicCommentBusinessDTO> comments = commentService.getDynamicHistoryComments(dynamicId, pageDTO);
        UserCommentsVO data = new UserCommentsVO();
        data.setComments(comments);
        data.setNextOffset(comments.isEmpty() ? commentOffset : comments.get(comments.size() - 1).getCommentId());
        response.setSuccess(data);
        return response;

    }

}
